define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/stockalert/index' + location.search,
                    handle_url: 'scanwork/stockalert/handle',
                    table: 'scanwork_stock_alert_log',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'material_code', title: '物料编码'},
                        {field: 'material_name', title: '物料名称'},
                        {field: 'spec', title: '规格'},
                        {field: 'warehouse_name', title: '仓库'},
                        {field: 'alert_type', title: '预警类型', searchList: {1:'低于最小库存',2:'高于最大库存',3:'即将过期',4:'已过期'}, formatter: Table.api.formatter.normal},
                        {field: 'current_quantity', title: '当前库存'},
                        {field: 'min_quantity', title: '最小库存'},
                        {field: 'max_quantity', title: '最大库存'},
                        {field: 'alert_level', title: '预警级别', searchList: {1:'高',2:'中',3:'低'}, formatter: function(value, row) {
                            var levelClass = {1: 'danger', 2: 'warning', 3: 'info'};
                            var levelText = {1: '高', 2: '中', 3: '低'};
                            return '<span class="label label-' + levelClass[value] + '">' + levelText[value] + '</span>';
                        }},
                        {field: 'is_handled', title: '处理状态', searchList: {0:'未处理',1:'已处理'}, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: '预警时间', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            buttons: [
                                {
                                    name: 'handle',
                                    text: '处理',
                                    title: '处理预警',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-check',
                                    url: 'scanwork/stockalert/handle',
                                    visible: function(row) {
                                        return row.is_handled == 0;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        handle: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

